package com.bw.a2209clubshop.ui.mall.view

import android.os.Bundle
import android.os.Handler
import android.os.Looper
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.viewModels
import androidx.lifecycle.Lifecycle
import androidx.recyclerview.widget.GridLayoutManager
import com.bw.a2209clubshop.R
import com.bw.a2209clubshop.base.BaseMallHomeDataFragment
import com.bw.a2209clubshop.base.state.Default
import com.bw.a2209clubshop.base.state.Empty
import com.bw.a2209clubshop.base.state.Failed
import com.bw.a2209clubshop.base.state.Loading
import com.bw.a2209clubshop.base.state.Success
import com.bw.a2209clubshop.databinding.FragmentMallDataBinding
import com.bw.a2209clubshop.mmkv.AuthMMKV
import com.bw.a2209clubshop.observeWithLifecycle
import com.bw.a2209clubshop.ui.mall.adapter.MallHomeMultiAdapter
import com.bw.a2209clubshop.ui.mall.model.MallModel
import com.bw.a2209clubshop.ui.mall.model.MallMotorSeries
import com.bw.a2209clubshop.ui.mall.model.MallRowTags
import com.bw.a2209clubshop.ui.mall.viewmodel.MallViewModel
import com.bw.a2209clubshop.utils.ErrorEmptyUIUtils
import com.bw.a2209clubshop.utils.Router.Ui.MallGroupGoodsListActivity
import javax.inject.Inject
/**
 *author：yjk
 *description:没用到！！！！！！！！
 *date：2025/5/20
*/
class MallDataFragment @Inject constructor() : BaseMallHomeDataFragment<MallViewModel, FragmentMallDataBinding>(){
    private val mViewModel: MallViewModel by viewModels()
    private val mHandler by lazy {
        Handler(Looper.getMainLooper())
    }
    private val mType: Int by lazy {
        arguments?.getInt(TYPE) ?: TYPE_HOME
    }
    companion object {
        private const val KEY_SERIES = "KEY_SERIES"
        private const val TYPE_HOME = 0
        private const val TYPE_MODEL = 1
        private const val TYPE_SERIES = 2
        private const val TYPE_DETAIL = 3

        // 0首页 1车型 2系列
        private const val TYPE = "TYPE"

        fun newInstance(series: MallMotorSeries?, ifSeries: Boolean = false, ifHome: Boolean = false, ifDetail: Boolean = false): MallDataFragment {
            val args = Bundle()
            if (series != null)
                args.putSerializable(KEY_SERIES, series)
            val type = if (ifHome) TYPE_HOME else if (ifSeries) TYPE_SERIES else if (ifDetail) TYPE_DETAIL else TYPE_MODEL
            args.putInt(TYPE, type)

            val fragment = MallDataFragment()
            fragment.arguments = args
            return fragment
        }
    }
    override fun initAdapter() {

        // 动态设置spanCount
        val spanCount = 2
        val layoutManager: GridLayoutManager = mBinding.rvMallHome.layoutManager as GridLayoutManager
        layoutManager.spanCount = spanCount
//        layoutManager.spanSizeLookup = object : GridLayoutManager.SpanSizeLookup() {
//            override fun getSpanSize(position: Int): Int {
//                return try {
//                    if (mItems.isEmpty())
//                        return 0
//                    mItems[position].getSpanSize()
//                } catch (e: Exception) {
//                    0
//                }
//            }
//        }
        mBinding.rvMallHome.layoutManager = layoutManager
        mBinding.rvMallHome.adapter=mAdapter

        super.bindAdapter(mBinding.rvMallHome)

    }

    override val mLayoutResId: Int
        get() = R.layout.fragment_mall_data

//    // 展示数据
//    override fun invalidateList(resultList: List<MallModel>, pages: Int) {
//        mBinding.rvMallHome.itemAnimator = null
//
//        mHandler.removeCallbacksAndMessages(null)
//        mHandler.postDelayed({
//            if (activity != null && !requireActivity().isFinishing) {
//                setLoadingViewVisible(false)
//                super.invalidateList(resultList, pages)
//            }
//        }, 666)
//    }
    override fun initData() {
        mBinding.viewEmptyUi.container.visibility = View.GONE
        getDataFromNet()

    }
    override fun getDataFromNet(onRefresh: Boolean) {
        super.getDataFromNet(onRefresh)
    }

}